package com.lekang.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.lekang.service.WorkBenchService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lekang.common.annotation.Log;
import com.lekang.common.core.controller.BaseController;
import com.lekang.common.core.domain.AjaxResult;
import com.lekang.common.enums.BusinessType;
import com.lekang.domain.Elderly;
import com.lekang.service.IElderlyService;
import com.lekang.common.utils.poi.ExcelUtil;
import com.lekang.common.core.page.TableDataInfo;

/**
 * 老人Controller
 * 
 * @author ruoyi
 * @date 2025-10-13
 */
@RestController
@RequestMapping("/system/elderly")
public class ElderlyController extends BaseController
{
    @Autowired
    private IElderlyService elderlyService;

    @Autowired
    private WorkBenchService workBenchService;

    /**
     * 查询老人列表
     */
    @PreAuthorize("@ss.hasPermi('system:elderly:list')")
    @GetMapping("/list")
    public TableDataInfo list(Elderly elderly)
    {
        startPage();
        List<Elderly> list = elderlyService.selectElderlyList(elderly);
        return getDataTable(list);
    }


    /**
     * 导出老人列表
     */
    @PreAuthorize("@ss.hasPermi('system:elderly:export')")
    @Log(title = "老人", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Elderly elderly)
    {
        List<Elderly> list = elderlyService.selectElderlyList(elderly);
        ExcelUtil<Elderly> util = new ExcelUtil<Elderly>(Elderly.class);
        util.exportExcel(response, list, "老人数据");
    }

    /**
     * 获取老人详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:elderly:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(elderlyService.selectElderlyById(id));
    }

    /**
     * 入退专用获取老人详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:elderly:query')")
    @GetMapping(value = "/rutui/{id}")
    public AjaxResult getInfo1(@PathVariable("id") Long id)
    {
        return success(elderlyService.selectElderlyById1(id));
    }

    /**
     * 新增老人
     */
    @PreAuthorize("@ss.hasPermi('system:elderly:add')")
    @Log(title = "老人", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Elderly elderly)
    {
//        return toAjax(elderlyService.insertElderly(elderly));若依原本代码
        // 1. 调用Service插入数据（插入后elderly的id会被自动回写）
        int rows = elderlyService.insertElderly(elderly);

        // 2. 判断插入是否成功，成功则返回主键id
        if (rows > 0) {
            // 关键：通过 elderly.getId() 获取回写的主键，封装到AjaxResult的data中
            return AjaxResult.success("新增老人成功").put("elderlyId", elderly.getId());
        }

        // 3. 插入失败返回错误信息
        return toAjax(rows);
    }

    /**
     * 修改老人
     */
    @PreAuthorize("@ss.hasPermi('system:elderly:edit')")
    @Log(title = "老人", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Elderly elderly)
    {
        return toAjax(elderlyService.updateElderly(elderly));
    }

    /**
     * 删除老人
     */
    @PreAuthorize("@ss.hasPermi('system:elderly:remove')")
    @Log(title = "老人", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(elderlyService.deleteElderlyByIds(ids));
    }

    /**
     * 解除老人与床位的绑定关系
     */
    @PreAuthorize("@ss.hasPermi('system:elderly:edit')")
    @Log(title = "老人", businessType = BusinessType.UPDATE)
    @PutMapping("/{elderlyId}/unbind-bed")
    public AjaxResult unbindBed(@PathVariable("elderlyId") Long elderlyId)
    {
        return toAjax(elderlyService.unbindBedFromElderly(elderlyId));
    }

    @GetMapping("/getCount")
    public AjaxResult getCount(){
        return success(workBenchService.countAllSysUser());
    }
}
